<template>
  <div class="rightbox" v-cloak>
    <h1>修改密码</h1>
    <div class="add-box" style="width:650px;">
      <el-form ref="form" :model="form" label-width="110px">
        <el-form-item
          prop="password"
          label="原始密码"
          required
          :rules="[
            { required: true, message: '请填写原始密码', trigger: 'blur' }
          ]"
        >
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item
          prop="passwordone"
          label="用户密码"
          required
          :rules="[
            { required: true, message: '请填写新密码', trigger: 'blur' }
          ]"
        >
          <el-input type="password" v-model="form.passwordone"></el-input>
        </el-form-item>
        <el-form-item
          prop="passwordtwo"
          label="确认密码"
          :rules="[
            { required: true, message: '请确认新密码', trigger: 'blur' }
          ]"
        >
          <el-input type="password" v-model="form.passwordtwo"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit('form')">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import qs from 'qs'
export default {
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.passwordone) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        password: '',
        passwordone: '',
        passwordtwo: ''
      },
      rules: {
        passwordtwo: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submit: function (form) {
      var that = this
      that.$refs[form].validate((valid) => {
        if (valid) {
          that.$pub.open()
          axios.post('/gp/Account/Modify', qs.stringify({
            Password: that.form.password,
            Passwordone: that.form.passwordone,
            Passwordtwo: that.form.passwordtwo
          })
          )
            .then((res) => {
              that.$pub.close()
              if (res.data.code === 0) {
                that.$alert('修改成功', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {
                    that.$router.push({
                      name: 'Welcome'
                    })
                  }
                })
              } else {
                that.$pub.failcode(that, res.data.code, res.data.data)
              }
            })
            .catch((res) => {
              that.$pub.catchbox(that)
            })
        } else {
          return false
        }
      })
    }
  },
  mounted () {
  }
}
</script>
